using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WPCoco
{
    /// <summary>
    /// World to View Matrix state stack
    /// </summary>
    public class WorldMatrix
    {
        #region Singleton

        static WorldMatrix shared;
        /// <summary>
        /// Current world-view matrix state
        /// </summary>
        public static WorldMatrix Shared
        {
            get 
            {
                if (shared == null)
                    shared = new WorldMatrix();
                return shared; 
            }
        }

        #endregion

        Stack<Matrix> stack = new Stack<Matrix>();

        Matrix currentMatrix = Matrix.Identity;

        public Matrix CurrentMatrix
        {
            get { return currentMatrix; }
            set { currentMatrix = value; }
        }

        public void Push(Matrix m) 
        {
            stack.Push(currentMatrix);
            Matrix.Multiply(ref m, ref currentMatrix, out currentMatrix);
        }

        public void Set(Matrix m) 
        {
            stack.Push(currentMatrix);
            currentMatrix = m;
        }

        public void Pop() 
        {
            currentMatrix = stack.Pop();
        }

        public void Begin() 
        {
            stack.Clear();
            currentMatrix = Matrix.Identity;
        }

        public void End() 
        {
            stack.Clear();
        }

        public int Count { get { return stack.Count + 1; } }
    }
}
